package com.bohua.Car.admin.controller;

import com.aliyun.oss.OSSClient;
import com.bohua.Car.admin.AliyunOSSClientUtil;
import com.bohua.Car.admin.Config.OSSClientConstants;
import com.bohua.Car.admin.common.Msg;
import com.bohua.Car.admin.entity.Carlook;
import com.bohua.Car.admin.service.CarlookService;
import com.bohua.Car.admin.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/tpurl")
public class UrlController {
    //阿里云API的内或外网域名
    private static String ENDPOINT;
    //阿里云API的密钥Access Key ID
    private static String ACCESS_KEY_ID;
    //阿里云API的密钥Access Key Secret
    private static String ACCESS_KEY_SECRET;
    //阿里云API的bucket名称
    private static String BACKET_NAME;
    //阿里云API的文件夹名称
    private static String FOLDER;

    //初始化属性
    static {
        ENDPOINT = OSSClientConstants.ENDPOINT;
        ACCESS_KEY_ID = OSSClientConstants.ACCESS_KEY_ID;
        ACCESS_KEY_SECRET = OSSClientConstants.ACCESS_KEY_SECRET;
        BACKET_NAME = OSSClientConstants.BACKET_NAME;
        FOLDER = OSSClientConstants.FOLDER;
    }

    @Resource
    private CarlookService carlookService;

    @RequestMapping("/upload")
    @ResponseBody
    public Msg upload(HttpServletRequest request, MultipartFile file, Carlook carlook) {
        Msg msg = new Msg();
        //		//初始化OSSClient
        OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
        // String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();//获取文件名称
        System.out.println("fileName-->" + fileName);
        //上传文件
        String files = "..\\Car\\Car-admin\\src\\main\\resources\\static\\images\\" + fileName;
        //存入数据库路径
        String hui="https://car-renting.oss-cn-beijing.aliyuncs.com/huishao/"+fileName;
        String[] filee = files.split(",");
        // System.out.println("getContentType-->" + contentType);
        /*        String filePath = "../Car/Car-admin/src/main/resources/static/images/";*/
        //获得服务路径
        System.out.println("路径" + files);
        for (String filename : filee) {
            try {
                //System.out.println("filename:"+filename);
                File filess = new File(filename);
                String md5key = AliyunOSSClientUtil.uploadObject2OSS(ossClient, filess, BACKET_NAME, FOLDER);
                System.out.println("上传后的文件MD5数字唯一签名" + md5key);
                //上传后的文件MD5数字唯一签名:40F4131427068E08451D37F02021473A
                msg.setMsg("上传成功");
                msg.setCode(1);
                msg.setData(hui);
            } catch (Exception e) {
                msg.setMsg("上传失败了!!");
            }
        }
        return msg;
    }
}
